const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const Product = sequelize.define('Product', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: '产品名称'
    },
    sku: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      comment: '产品编码'
    },
    category_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '分类ID'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '产品描述'
    },
    price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '销售价格'
    },
    cost: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: '成本价格'
    },
    unit: {
      type: DataTypes.STRING(20),
      defaultValue: '个',
      comment: '计量单位'
    },
    weight: {
      type: DataTypes.DECIMAL(8, 3),
      allowNull: true,
      comment: '重量(kg)'
    },
    dimensions: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '尺寸(长*宽*高)'
    },
    images: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '产品图片URL数组'
    },
    attributes: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '产品属性JSON'
    },
    tags: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '产品标签数组'
    },
    status: {
      type: DataTypes.ENUM('active', 'inactive', 'discontinued'),
      defaultValue: 'active',
      comment: '产品状态'
    },
    is_featured: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      comment: '是否推荐产品'
    },
    min_stock: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '最小库存警戒线'
    },
    max_stock: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '最大库存'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '创建者ID'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'products',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '产品表',
    indexes: [
      {
        unique: true,
        fields: ['sku']
      },
      {
        fields: ['category_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['name']
      }
    ]
  })

  return Product
}